/*
 * IoService.h
 *
 *  Created on: Nov 4, 2012
 *      Author: work
 */

#ifndef IOSERVICE_H_
#define IOSERVICE_H_

#include <list>
#include <vector>
#include <unordered_map>
#include <sys/epoll.h>

#include "base/Types.h"
#include "Operation.hpp"
#include "Channel.h"

namespace zeus
{

class IoService:public NonCopyAble
{
public:

	typedef std::function<void ()> EmptyHandler;

private:

	bool _is_running;

	Channels _channels;

	Operations _read_ops;
	Operations _write_ops;

	Operations _complete_ops;

private:

	int32 _epoll_fd;

public:
	IoService();
	virtual ~IoService();

public:

	void loop();

	void quit();

public:

	void wake();

	void post(const EmptyHandler & handler);

public:

	void read_op(int32 fd,Operation * op);

	void write_op(int32 fd,Operation * op);

private:

	void epoll_add(Channel & channel,int32 event);

	void epoll_mod(Channel & channel,int32 event);

	void poll(int32 wait_ms);

};

} /* namespace zeus */
#endif /* IOSERVICE_H_ */
